﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SATF.BI;
using ExtAspNet;

namespace SATF
{
    public partial class HomePage : System.Web.UI.Page
    {
        private BIUser user = null;
        public static BITasks mytasks = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            //通过session验证是否登录
            if (Session["user"] == null || Session["user"].ToString() == "")
            {
                return;
            }
            string userName = Session["user"].ToString();

            if (!IsPostBack)
            {
                user = LogonInfosSingleton.instance[userName].BIUser;
                mytasks = user.GetMyTasks();
            }

            this.myTaskGrid.DataSource = mytasks;
            this.myTaskGrid.DataBind();
        }

        protected string GetExecState(object state)
        {
            return BIConverters.MyTaskState(Convert.ToInt32(state));
        }

        protected System.Drawing.Color GetExecStateColor(object state)
        {
            switch (Convert.ToInt32(state))
            {
                case 1:
                    return System.Drawing.Color.LightGreen; //正在运行
                case 2:
                    return System.Drawing.Color.DarkBlue; //完成
                case 3:
                    return System.Drawing.Color.Yellow;  //暂停
                case 4:
                    return System.Drawing.Color.Red;  //停止
                default:
                    return System.Drawing.Color.Gray;  //空
            }
        }

        protected void MyTaskGrid_RowClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            Alert.ShowInTop(String.Format("你点击了第 {0} 行（双击）", e.RowIndex + 1));
        }

        protected void RunLastTaskBtn_Clicked(object sender, EventArgs e)
        {
            //mytasks[1].Name = "我的任务改变了";
            //Alert.ShowInTop("暂未实现");
            //extaspnet表格控件需要显示的重新绑定，有点麻烦，每次page_load都重新绑定一次
            //this.myTaskGrid.DataSource = mytasks;
            //this.myTaskGrid.DataBind();
            if (!AssertIsLogon())
            {
                Alert.ShowInTop("您尚未登录!");
                return;
            }

            BITasks tasks = (BITasks)this.myTaskGrid.DataSource;
            tasks[1].Name = "我的任务改变了";

        }

        protected void CopyCreateBtn_Clicked(object sender, EventArgs e)
        {
            Alert.ShowInTop("暂未实现!");

        }

        private bool AssertIsLogon()
        {
            if (Session["user"] == null || Session["user"].ToString() == "")
            {
                return false;
            }
            string userName = Session["user"].ToString();

            LogonInfo info = LogonInfosSingleton.instance[userName];
            if (info == null || !info.IsLogon)
            {
                return false;
            }
            return true;
        }



    }
}